define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'domain/index',
                    add_url: 'domain/add',
                    edit_url: 'domain/edit',
                    del_url: 'domain/del',
                    multi_url: 'domain/multi',
                    table: 'domain',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'domain_title', title: '域名标题',},
                        {field: 'domain_url', title: '域名',},
                        {field: 'code', title: '编码',},
                        {field: 'address', title: '地址',},
                        {field: 'longitude', title: '经度',visible:false},
                        {field: 'latitude', title: '维度',visible:false},
                        {field: 'mobile', title: '手机号',},
                        {field: 'url_status', title: __('状态'),
                            searchList: {
                                "1": __('开启'),
                                "0": __('关闭'),
                            },
                            formatter: function (value){
                                switch (value) {
                                    case 0:
                                        value='关闭';
                                        break;
                                    case 1:
                                        value='开启';
                                        break;

                                }
                                return value;
                            }
                        },
                        {field: 'logo', title: __('Logo'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {
                            field: 'create_time',
                            title: __('添加时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                // {
                                //     name: 'city',
                                //     text: __('配置对应内容'),
                                //     title: __('配置对应内容'),
                                //     classname: 'btn btn-xs btn-success btn-magic btn-addtabs',
                                //     icon: 'fa fa-eye',
                                //     url: 'train/train/content?title_id={id}',
                                //     // extend:'data-area=["60%","70%"]',
                                //     callback: function (data) {
                                //     },
                                // },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
             $(document).on('click', "[data-toggle='addresspicker']", function () {
                                            var that = this;
                                            var callback = $(that).data('callback');
                                            var input_id = $(that).data("input-id") ? $(that).data("input-id") : "";
                                            var lat_id = $(that).data("lat-id") ? $(that).data("lat-id") : "";
                                            var lng_id = $(that).data("lng-id") ? $(that).data("lng-id") : "";
                                            var lat = lat_id ? $("#" + lat_id).val() : '';
                                            var lng = lng_id ? $("#" + lng_id).val() : '';
                                            var url = "/addons/address/index/select";
                                            url += (lat && lng) ? '?lat=' + lat + '&lng=' + lng : '';
                                            Fast.api.open(url, '位置选择', {
                                                callback: function (res) {
                                                    input_id && $("#" + input_id).val(res.address);
                                                    lat_id && $("#" + lat_id).val(res.lat);
                                                    lng_id && $("#" + lng_id).val(res.lng);
                                                    try {
                                                        //执行回调函数
                                                        if (typeof callback === 'function') {
                                                            callback.call(that, res);
                                                        }
                                                    } catch (e) {

                                                    }
                                                }
                                            });
                                        });
        },
        edit: function () {
            Controller.api.bindevent();
             $(document).on('click', "[data-toggle='addresspicker']", function () {
                                            var that = this;
                                            var callback = $(that).data('callback');
                                            var input_id = $(that).data("input-id") ? $(that).data("input-id") : "";
                                            var lat_id = $(that).data("lat-id") ? $(that).data("lat-id") : "";
                                            var lng_id = $(that).data("lng-id") ? $(that).data("lng-id") : "";
                                            var lat = lat_id ? $("#" + lat_id).val() : '';
                                            var lng = lng_id ? $("#" + lng_id).val() : '';
                                            var url = "/addons/address/index/select";
                                            url += (lat && lng) ? '?lat=' + lat + '&lng=' + lng : '';
                                            Fast.api.open(url, '位置选择', {
                                                callback: function (res) {
                                                    input_id && $("#" + input_id).val(res.address);
                                                    lat_id && $("#" + lat_id).val(res.lat);
                                                    lng_id && $("#" + lng_id).val(res.lng);
                                                    try {
                                                        //执行回调函数
                                                        if (typeof callback === 'function') {
                                                            callback.call(that, res);
                                                        }
                                                    } catch (e) {

                                                    }
                                                }
                                            });
                                        });
        },

        content: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'train/train/content',
                    add_url: 'train/train/add_content',
                    edit_url: 'train/train/edit_content',
                    del_url: 'train/train/del_content',
                    multi_url: 'train/train/multi',
                    table: 'content',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        {field: 'train_title', title: '内培标题',},
                        {field: 'train.title', title: '类型名称',},
                        {field: 'train_status', title: __('状态'),
                            searchList: {
                                "1": __('开启'),
                                "0": __('关闭'),
                            },
                            formatter: function (value){
                                switch (value) {
                                    case 0:
                                        value='关闭';
                                        break;
                                    case 1:
                                        value='开启';
                                        break;

                                }
                                return value;
                            }
                        },
                        {
                            field: 'create_time',
                            title: __('添加时间'),
                            formatter: Table.api.formatter.datetime,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            sortable: true
                        },
                        {
                            field: 'operate', title: __('Operate'), table: table, buttons: [
                                // {
                                //     name: 'city',
                                //     text: __('配置对应内容'),
                                //     title: __('配置对应内容'),
                                //     classname: 'btn btn-xs btn-success btn-magic btn-addtabs',
                                //     icon: 'fa fa-eye',
                                //     url: 'train/train/content?title_id={id}',
                                //     // extend:'data-area=["60%","70%"]',
                                //     callback: function (data) {
                                //     },
                                // },
                            ], events: Table.api.events.operate, formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                $(table).data("operate-edit", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                $(table).data("operate-del", true); // 列表页面隐藏 .编辑operate-edit  - 删除按钮operate-del
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add_content: function () {
            Controller.api.bindevent();
        },
        edit_content: function () {
            Controller.api.bindevent();
        },
        //选择校内班级
        select_school_class: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trusteeship/my_class/select_school_class' + location.search,
                }
            });


            var table = $("#table");
            // 初始化表格

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', visible: false, title: __('班级id'),},
                        {field: 'name', title: __('班级名称'), operate: false},
                    ]
                ]
            });

            // 同步微信小程序后台直播
            let parenttable = table.closest('.bootstrap-table');
            let options = table.bootstrapTable('getOptions');
            let toolbar = $(options.toolbar, parenttable);
            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var urlArr = new Array();
                var now_arr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    //   now_arr=[j.goods_id,j.goods_name];
                    urlArr.push(j.id);
                    now_arr.push(j.name);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;

                Fast.api.close({id: urlArr.join(","), name: now_arr.join(","), multiple: true});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        //选择班主任
        select_teacher: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trusteeship/my_class/select_teacher' + location.search,
                }
            });


            var table = $("#table");
            // 初始化表格
            var str = {
                1: "全国",
                2: "北京",
                3: "福建",
                4: "浙江",
                5: "河南",
                6: "安徽",
                7: "上海",
                8: "江苏",
                9: "山东",
                10: "江西",
                11: "重庆",
                12: "湖南",
                13: "湖北",
                14: "广东",
                15: "广西",
                16: "贵州",
                17: "海南",
                18: "四川",
                19: "云南",
                20: "陕西",
                21: "甘肃",
                22: "宁夏",
                23: "青海",
                24: "新疆",
                25: "西藏",
                26: "天津",
                27: "黑龙江",
                28: "吉林",
                29: "辽宁",
                30: "河北",
                31: "山西",
                32: "内蒙古"
            };

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', visible: false, title: __('id'),},
                        {field: 'nickname', title: __('班主任名称'), operate: false},
                    ]
                ]
            });

            // 同步微信小程序后台直播
            let parenttable = table.closest('.bootstrap-table');
            let options = table.bootstrapTable('getOptions');
            let toolbar = $(options.toolbar, parenttable);
            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var urlArr = new Array();
                var now_arr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    //   now_arr=[j.goods_id,j.goods_name];
                    urlArr.push(j.id);
                    now_arr.push(j.nickname);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;

                Fast.api.close({id: urlArr.join(","), name: now_arr.join(","), multiple: true});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        //选择教室
        select_class_room: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'trusteeship/my_class/select_class_room' + location.search,
                }
            });


            var table = $("#table");
            // 初始化表格
            var str = {
                1: "全国",
                2: "北京",
                3: "福建",
                4: "浙江",
                5: "河南",
                6: "安徽",
                7: "上海",
                8: "江苏",
                9: "山东",
                10: "江西",
                11: "重庆",
                12: "湖南",
                13: "湖北",
                14: "广东",
                15: "广西",
                16: "贵州",
                17: "海南",
                18: "四川",
                19: "云南",
                20: "陕西",
                21: "甘肃",
                22: "宁夏",
                23: "青海",
                24: "新疆",
                25: "西藏",
                26: "天津",
                27: "黑龙江",
                28: "吉林",
                29: "辽宁",
                30: "河北",
                31: "山西",
                32: "内蒙古"
            };

            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', visible: false, title: __('id'),},
                        {field: 'classroom', title: __('教室名称'), operate: false},
                    ]
                ]
            });

            // 同步微信小程序后台直播
            let parenttable = table.closest('.bootstrap-table');
            let options = table.bootstrapTable('getOptions');
            let toolbar = $(options.toolbar, parenttable);
            // 选中多个
            $(document).on("click", ".btn-choose-multi", function () {
                var urlArr = new Array();
                var now_arr = new Array();
                $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                    //   now_arr=[j.goods_id,j.goods_name];
                    urlArr.push(j.id);
                    now_arr.push(j.classroom);
                });
                var multiple = Backend.api.query('multiple');
                multiple = multiple == 'true' ? true : false;

                Fast.api.close({id: urlArr.join(","), name: now_arr.join(","), multiple: true});
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
                //选择教室
                select_student: function () {
                    // 初始化表格参数配置
                    Table.api.init({
                        extend: {
                            index_url: 'reception/sheet/select_student' + location.search,
                        }
                    });


                    var table = $("#table");
                    // 初始化表格


                    table.bootstrapTable({
                        url: $.fn.bootstrapTable.defaults.extend.index_url,
                        pk: 'id',
                        sortName: 'id',
                        columns: [
                            [
                                { checkbox: true },
                                { field: 'id', visible: false, title: __('id'), },
                                { field: 'student_no', title: '学号', },
                                { field: 'student_name', title: '学生名称' },
                                { field: 'phone', title: '手机号' },
                                {
                                    field: 'sex', title: __('性别'),
                                    searchList: {
                                        "1": __('男'),
                                        "2": __('女'),
                                    }, formatter: function (value) {
                                        switch (value) {
                                            case 1:
                                                value = '男';
                                                break;
                                            case 2:
                                                value = '女';
                                                break;
                                        }
                                        return value;
                                    }
                                },
                                { field: 'parents', title: '家长名称' },
                                { field: 'school_class', title: '家长名称', visible: false },
                                { field: 'school_class_id', title: '家长名称', visible: false },
                                {
                                    field: 'image',
                                    title: __('学生照片'),
                                    events: Table.api.events.image,
                                    formatter: Table.api.formatter.image,
                                    operate: false
                                },
                            ]
                        ]
                    });

                    // 同步微信小程序后台直播
                    let parenttable = table.closest('.bootstrap-table');
                    let options = table.bootstrapTable('getOptions');
                    let toolbar = $(options.toolbar, parenttable);
                    // 选中多个
                    $(document).on("click", ".btn-choose-multi", function () {
                        var urlArr = new Array();
                        var now_arr = new Array();
                        var phone_arr = new Array();
                        var sex_arr = new Array();
                        var parents_arr = new Array();
                        var image_arr = new Array();
                        var school_class_id_arr = new Array();
                        var school_class_arr = new Array();
                        $.each(table.bootstrapTable("getAllSelections"), function (i, j) {
                            //   now_arr=[j.goods_id,j.goods_name];
                            urlArr.push(j.id);
                            now_arr.push(j.student_name);
                            phone_arr.push(j.phone);
                            sex_arr.push(j.sex);
                            image_arr.push(j.image);
                            parents_arr.push(j.parents);
                            school_class_id_arr.push(j.school_class_id);
                            school_class_arr.push(j.school_class);
                        });
                        var multiple = Backend.api.query('multiple');
                        multiple = multiple == 'true' ? true : false;
                        var new_data = {
                            id: urlArr.join(","),
                            student_name: now_arr.join(","),
                            phone: phone_arr.join(","),
                            sex: sex_arr.join(","),
                            parents: parents_arr.join(","),
                            image: image_arr.join(","),
                            school_class: school_class_arr.join(","),
                            school_class_id_arr: school_class_id_arr.join(","),
                            multiple: true
                        };
                        Fast.api.close(new_data);
                    });

                    // 为表格绑定事件
                    Table.api.bindevent(table);
                },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});

function select_school_class() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();

    parent.Fast.api.open("trusteeship/my_class/select_school_class?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id)
            console.log(data.name)
            $("#school_class_id").val(data.id)
            $("#school_class").val(data.name)
        }
    });
}

function select_teacher() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();

    parent.Fast.api.open("trusteeship/my_class/select_teacher?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id)
            console.log(data.name)
            $("#teacher_id").val(data.id)
            $("#teacher").val(data.name)
        }
    });
}

function select_class_room() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();
    if (!$("#plan_name").val()) {
        $("#store_id").val(0)
        store_id = 0;
    }
    parent.Fast.api.open("trusteeship/my_class/select_class_room?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id)
            console.log(data.name)
            $("#classroom_id").val(data.id)
            $("#classroom").val(data.name)
        }
    });
}
function select_student() {
    var that = this;
    var opt = $("select[name='row[user_type]']").val();
    var multiple = $(this).data("multiple") ? $(this).data("multiple") : false;
    var mimetype = $(this).data("mimetype") ? opt : 0;
    var admin_id = $(this).data("admin-id") ? $(this).data("admin-id") : '';
    var user_id = $(this).data("user-id") ? $(this).data("user-id") : '';
    var teacher_id = $("#teacher_id").val();
    var school_id = $("#school_id").val();
    parent.Fast.api.open("reception/sheet/select_student?element_id=" + teacher_id + "&school_id=" + school_id + "&multiple=" + multiple + "&mimetype=" + mimetype + "&admin_id=" + admin_id + "&user_id=" + user_id, __('Choose'), {
        callback: function (data) {
            console.log(data.id);
            console.log(data.name);
            $("#student_id").val(data.id);
            $("#student_name").val(data.student_name);
            $("#student_name1").val(data.student_name);
            $("#student_name2").val(data.student_name);
            $("#c-image").val(data.image);
            $("#phone").val(data.phone);
            $("#parents").val(data.parents);
            $("#school_class_id").val(data.school_class_id);
            $("#school_class").val(data.school_class);
        }
    });
}